{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7.22 Compressible Fluids at Subsonic Velocity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Given that air is at a pressure of 19.3 psig and a temperature of 100 deg F and is flowing out of a 1/2\" schedule 80 pipe into the atmosphere.\n",
    "Find the flow rate of air in standard cubic feet per hour."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from fluids.units import *\n",
    "from math import pi\n",
    "dP = 19.3*u.psi\n",
    "P = dP + 1*u.atm\n",
    "P2 =  1*u.atm\n",
    "T = 100*u.degF\n",
    "L = 10*u.foot\n",
    "NPS, D_pipe, Do_pipe, t = nearest_pipe(NPS=0.5, schedule=80)\n",
    "A = 0.25*pi*D_pipe**2\n",
    "\n",
    "fd = 0.0275 # assumed, initial guess\n",
    "mu = 1.8e-8*u.Pa*u.s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ratio of actual to standard flow: 0.4663198131346627\n",
      "Air density: 2.6183038705121247 kilogram / meter ** 3\n"
     ]
    }
   ],
   "source": [
    "# Take nitrogen-oxygen as air, and find the density and ratio\n",
    "from chemicals import Vm_to_rho\n",
    "from thermo import PRMIX\n",
    "zs = [0.79, 0.21]\n",
    "Tcs = [126.2, 154.58]\n",
    "Pcs = [3394387.5, 5042945.25]\n",
    "omegas = [0.04, 0.021]\n",
    "MWs = [28.0134, 31.9988]\n",
    "MW = sum(MWs[i]*zs[i] for i in range(2))\n",
    "\n",
    "eos_flowing = PRMIX(T=(T).to(u.K).magnitude, P=P.to(u.Pa).magnitude, zs=zs, Tcs=Tcs, Pcs=Pcs, omegas=omegas)\n",
    "eos_std = PRMIX(T=288.15, P=101325.0, zs=zs, Tcs=Tcs, Pcs=Pcs, omegas=omegas)\n",
    "V_ratio = eos_flowing.V_g/eos_std.V_g\n",
    "print('Ratio of actual to standard flow: %s' %(V_ratio))\n",
    "\n",
    "rho = Vm_to_rho(eos_flowing.V_g, MW)*u.kg/u.m**3\n",
    "print('Air density: %s' %(rho))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Flow rate = 3773.572213074693 foot ** 3 / hour\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    # Problem says to consider 1 exit, and compressible friction\n",
    "    K = K_from_f(fd=fd, L=L, D=D_pipe)\n",
    "    K += exit_normal()\n",
    "    # lump the two losses together for the `isothermal_gas` function\n",
    "    fd_tot = f_from_K(L=L, D=D_pipe, K=K)\n",
    "    m = isothermal_gas(rho=rho, fd=fd_tot, P1=P, P2=P2, L=L, D=D_pipe)\n",
    "    Q = m/rho\n",
    "    v = Q/A\n",
    "    # update frictoin factor\n",
    "    Re = Reynolds(D=D_pipe, rho=rho, mu=mu, V=v)\n",
    "    fd = friction_factor(Re=Re, eD=0.0018*u.inch/D_pipe)\n",
    "    Q_std = Q/V_ratio\n",
    "print('Flow rate = %s' %(Q_std.to(u.ft**3/u.hour)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The solution given in Crane is 3762 scfh. The solution there uses a simpler formula and does not iterate to converge the friction factor."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
